package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.pictureSelector.SelectPicture;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.uploadPicture.IUploadPicture;
import com.fgs.common.uploadPicture.UploadPicture;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.luck.picture.lib.entity.LocalMedia;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.mvp.model.AddTaiTouBean;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.other.EventWhat;
import com.tianhui.consignortwofei.other.PictureType;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/28
 * @description
 **/
public class AddTaiTouActivity extends BaseActivity {

    @BindView(R.id.iiv_danhao)
    InputItemView iiv_danhao;
    @BindView(R.id.iiv_yuan_name)
    InputItemView iiv_yuan_name;
    @BindView(R.id.iiv_deng_name)
    InputItemView iiv_deng_name;
    @BindView(R.id.iiv_huoquan)
    InputItemView iiv_huoquan;
    @BindView(R.id.iiv_money)
    InputItemView iiv_money;

    @BindView(R.id.civ_danju_time)
    ClickItemView civ_danju_time;
    @BindView(R.id.civ_danju_gong)
    ClickItemView civ_danju_gong;
    @BindView(R.id.civ_danju_xianjin)
    ClickItemView civ_danju_xianjin;

    @BindView(R.id.rl_hetong)
    RecyclerView rl_hetong;
    @BindView(R.id.rl_jinkuan)
    RecyclerView rl_jinkuan;



    @Override
    public int getLayoutId() {
        return R.layout.activity_add_taitou;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("添加进款信息");
        driverModel=new DriverModel();

        initPictureOne();
        initPictureTwo();

    }



    @Override
    public BaseFragment createFragment() {
        return null;
    }

    @OnClick(R.id.btn_true)
    public void addTrue() {

        String danhao = iiv_danhao.getContent();
        if (TextUtils.isEmpty(danhao)){
            showToast("销货单号不能为空");
            return;
        }
        if (danhao.length()<13){
            showToast("销货单号长度不能小于14个字符");
            return;
        }

        String yuanName = iiv_yuan_name.getContent();
        if (TextUtils.isEmpty(yuanName)){
            showToast("原登记客户名称不能为空");
            return;
        }

        String dengName = iiv_deng_name.getContent();
        if (TextUtils.isEmpty(dengName)){
            showToast("进款登记客户名称不能为空");
            return;
        }

        String huoquan = iiv_huoquan.getContent();
        if (TextUtils.isEmpty(huoquan)){
            showToast("进款货权不能为空");
            return;
        }

        String money = iiv_money.getContent();
        if (TextUtils.isEmpty(money)){
            showToast("进款金额不能为空");
            return;
        }

        String danjuTime = civ_danju_time.getContent();
        if (TextUtils.isEmpty(danjuTime)){
            showToast("单据日期未选择");
            return;
        }

        String danjuGong = civ_danju_gong.getContent();
        String danjuXianJin = civ_danju_xianjin.getContent();

        String pictureOne="";
        if (mSelectPictureList.size()>0){
            pictureOne = mSelectPictureList.get(0);
        }

        String pictureTwo="";
        if (mSelectPictureListTwo.size()>0){
            pictureTwo = mSelectPictureListTwo.get(0);
        }

        AddTaiTouBean taiTouBean=new AddTaiTouBean(danhao,yuanName,dengName,money,
                danjuTime,danjuGong,danjuXianJin,huoquan,pictureOne,pictureTwo,"");


        addTaiTouNew(taiTouBean);

    }


    private TimePickerView mDeadlineSelectTimeDialog;
    @OnClick(R.id.civ_danju_time)
    public void selectDeadlineTimeOne() {
        hideKeyboard();
        if (mDeadlineSelectTimeDialog == null) {
            mDeadlineSelectTimeDialog = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    civ_danju_time.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mDeadlineSelectTimeDialog.show();


    }


    private TimePickerView mDeadlineSelectTimeDialogTwo;
    @OnClick(R.id.civ_danju_gong)
    public void selectDeadlineTimeTwo() {
        hideKeyboard();
        if (mDeadlineSelectTimeDialogTwo == null) {
            mDeadlineSelectTimeDialogTwo = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    civ_danju_gong.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mDeadlineSelectTimeDialogTwo.show();

    }


    private TimePickerView mDeadlineSelectTimeDialogThree;
    @OnClick(R.id.civ_danju_xianjin)
    public void selectDeadlineTimeThree() {
        hideKeyboard();
        if (mDeadlineSelectTimeDialogThree == null) {
            mDeadlineSelectTimeDialogThree = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    civ_danju_xianjin.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mDeadlineSelectTimeDialogThree.show();

    }



    //图片上传1
    private SelectPicture mSelectPicture;
    private List<String> mSelectPictureList = new ArrayList<>();
    private void initPictureOne() {
        mSelectPicture = new SelectPicture();
        mSelectPicture.setMaxSelectNum(1);

        mSelectPicture.selectMultiPicture(this, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

                if (null != selectList && selectList.size() > 0) {

                    mSelectPictureList.clear();
                    int size = selectList.size();
                    String path = "";
                    for (int i = 0; i < size; i++) {
                        path = selectList.get(i).getCompressPath();
                        requestPictureUpload(PictureType.TYPE_INVOICES_IMAGE, path, i);
                    }
                }
            }

            @Override
            public void selectResultPath(String path) {

            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
                int size = mSelectPictureList.size();
                LogUtil.print("position--" + position + "\n" + "size--" + size);
                if (position < size) {
                    mSelectPictureList.remove(position);
                }

            }
        }, rl_hetong);
    }


    private void requestPictureUpload(int type, String path, int position) {
        if (TextUtils.isEmpty(path)) return;
//        String module = getPictureModule();
//        if (TextUtils.isEmpty(module)) {
//            module = "orderEvaluate";
//        }
        String module ="orderEvaluate";

        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.picturePath = path;
        uploadPictureInfo.pictureModule = module;

        new UploadPicture(new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                processPictureUploadResult(pictureInfo, type);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {
                removePicture(type, position);
            }
        }).uploadPicture(this, uploadPictureInfo);

    }

    private void removePicture(int type, int position) {
        if (type == PictureType.TYPE_INVOICES_IMAGE) {
            mSelectPicture.removePicture(position);
        }
    }

    private void processPictureUploadResult(PictureInfo pictureInfo, int type) {
        switch (type) {
            case PictureType.TYPE_INVOICES_IMAGE:
                mSelectPictureList.add(pictureInfo.realFileName);
                break;
        }
        LogUtil.print("pictureID---" + pictureInfo.realFileName);
    }





    //图片上传2
    private SelectPicture mSelectPictureTwo;
    private List<String> mSelectPictureListTwo = new ArrayList<>();
    private void initPictureTwo() {
        mSelectPictureTwo = new SelectPicture();
        mSelectPictureTwo.setMaxSelectNum(1);

        mSelectPictureTwo.selectMultiPicture(this, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

                if (null != selectList && selectList.size() > 0) {

                    mSelectPictureListTwo.clear();
                    int size = selectList.size();
                    String path = "";
                    for (int i = 0; i < size; i++) {
                        path = selectList.get(i).getCompressPath();
                        requestPictureUploadTwo(PictureType.TYPE_INVOICES_IMAGE, path, i);
                    }
                }
            }

            @Override
            public void selectResultPath(String path) {

            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
                int size = mSelectPictureListTwo.size();
                LogUtil.print("position--" + position + "\n" + "size--" + size);
                if (position < size) {
                    mSelectPictureListTwo.remove(position);
                }

            }
        }, rl_jinkuan);
    }



    private void requestPictureUploadTwo(int type, String path, int position) {
        if (TextUtils.isEmpty(path)) return;
//        String module = getPictureModule();
//        if (TextUtils.isEmpty(module)) {
//            module = "orderEvaluate";
//        }
        String module ="orderEvaluate";

        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.picturePath = path;
        uploadPictureInfo.pictureModule = module;

        new UploadPicture(new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                processPictureUploadResultTwo(pictureInfo, type);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {
                removePictureTwo(type, position);
            }
        }).uploadPicture(this, uploadPictureInfo);

    }

    private void removePictureTwo(int type, int position) {
        if (type == PictureType.TYPE_INVOICES_IMAGE) {
            mSelectPictureTwo.removePicture(position);
        }
    }

    private void processPictureUploadResultTwo(PictureInfo pictureInfo, int type) {
        switch (type) {
            case PictureType.TYPE_INVOICES_IMAGE:
                mSelectPictureListTwo.add(pictureInfo.realFileName);
                break;
        }
        LogUtil.print("pictureID---" + pictureInfo.realFileName);
    }






    private DriverModel driverModel;
    private void addTaiTouNew(AddTaiTouBean taiTouBean) {

        driverModel.addTaiTouNew(getContext(), taiTouBean, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_REFRESH_TAITOU_LIST.getValue()));
                    ToastUtil.showShortToast("添加成功");
                    finish();
                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }









}
